﻿Public Class MediaInfoDetailsControl

    Public Sub UpdateView(ByVal mediaInfo As Entities.MediaInfo)
        Dim clsSb As New System.Text.StringBuilder
        If mediaInfo IsNot Nothing AndAlso mediaInfo.OpenMediaInfo IsNot Nothing Then
            Dim mi As Entities.OpenMediaInfo.OpenMediaInfo = mediaInfo.OpenMediaInfo

            If mi.Feature IsNot Nothing Then
                AddTitle(clsSb, "General Info")
                AddInfoLine(clsSb, "Title", mi.Feature.Title)
                AddInfoLine(clsSb, "Year", mi.Feature.Year)
                AddInfoLine(clsSb, "Ids", Framework.Serialization.ToCommaSepString(mi.Feature.Ids))
            End If

            If mi.Streams IsNot Nothing AndAlso mi.Streams.Count > 0 Then
                Dim clsStreamInfo As Entities.OpenMediaInfo.StreamInfo = mi.Streams(0)

                AddInfoLine(clsSb, "Duration", clsStreamInfo.Runtime)
                AddInfoLine(clsSb, "Overall bitrate", clsStreamInfo.OverallBitrate)

                If clsStreamInfo.VideoStreams IsNot Nothing AndAlso clsStreamInfo.VideoStreams.Count > 0 Then
                    AddEmptyLine(clsSb)
                    AddTitle(clsSb, "Video")
                    For Each clsTrack As Entities.OpenMediaInfo.VideoStreamInfo In clsStreamInfo.VideoStreams
                        AddInfoLine(clsSb, "Dimensions", String.Format("{0}x{1}", clsTrack.Width, clsTrack.Height))
                        AddInfoLine(clsSb, "AspectRatio", clsTrack.AspectRatio)
                        AddInfoLine(clsSb, "Bitrate", (clsTrack.BitrateMode & " " & clsTrack.Bitrate).Trim)
                        AddInfoLine(clsSb, "Bits/(Pixel*Frame)", clsTrack.BitsPerPixelFrame)
                        AddInfoLine(clsSb, "Format", clsTrack.Format & " (" & clsTrack.FormatInfo & ")")
                        AddInfoLine(clsSb, "FormatProfile", clsTrack.FormatProfile)
                        AddInfoLine(clsSb, "Framerate", clsTrack.Framerate)
                        AddInfoLine(clsSb, "Language", clsTrack.Language & "(" & clsTrack.LanguageInfo & ")")
                    Next
                End If

                If clsStreamInfo.AudioStreams IsNot Nothing AndAlso clsStreamInfo.AudioStreams.Count > 0 Then
                    AddEmptyLine(clsSb)
                    AddTitle(clsSb, "Audio")
                    For Each clsTrack As Entities.OpenMediaInfo.AudioStreamInfo In clsStreamInfo.AudioStreams
                        AddInfoLine(clsSb, "Bitrate", (clsTrack.BitrateMode & " " & clsTrack.Bitrate).Trim)
                        AddInfoLine(clsSb, "Format", clsTrack.Format & " (" & clsTrack.FormatInfo & ")")
                        AddInfoLine(clsSb, "FormatProfile", clsTrack.FormatProfile)
                        AddInfoLine(clsSb, "Language", clsTrack.Language & "(" & clsTrack.LanguageInfo & ")")
                        AddInfoLine(clsSb, "Channels", clsTrack.Channels & " (" & clsTrack.ChannelPositions & ")")
                    Next
                End If

                If clsStreamInfo.SubtitleStreams IsNot Nothing AndAlso clsStreamInfo.SubtitleStreams.Count > 0 Then
                    AddEmptyLine(clsSb)
                    AddTitle(clsSb, "Subtitles")
                    For Each clsTrack As Entities.OpenMediaInfo.SubtitleStreamInfo In clsStreamInfo.SubtitleStreams
                        AddInfoLine(clsSb, "Format", clsTrack.Format & " (" & clsTrack.FormatInfo & ")")
                        AddInfoLine(clsSb, "Language", clsTrack.Language & "(" & clsTrack.LanguageInfo & ")")
                    Next
                End If
            End If
        End If
        txtTypeInfo.Text = clsSb.ToString
    End Sub

    Private Sub AddInfoLine(ByVal buffer As System.Text.StringBuilder, ByVal label As String, ByVal text As String)
        buffer.AppendFormat("{0}:", label)
        If Not String.IsNullOrEmpty(text) Then
            If label.Length < 7 Then buffer.Append(vbTab)
            If label.Length < 15 Then buffer.Append(vbTab)
            buffer.Append(vbTab)
            buffer.Append(text.Replace("()", String.Empty))
        End If
        buffer.AppendLine()
    End Sub
    Public Sub AddEmptyLine(ByVal buffer As System.Text.StringBuilder)
        buffer.AppendLine()
    End Sub
    Public Sub AddTitle(ByVal buffer As System.Text.StringBuilder, ByVal title As String)
        buffer.AppendLine(title)
    End Sub
End Class
